package withdraw

import (
	"context"
	"encoding/json"

	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/types"
	"gitee.com/liuxuezhan/ar-platform/assets-api/pkg/code"
	"gitee.com/liuxuezhan/ar-platform/assets-api/pkg/common"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/dataview"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/rpc"
	"github.com/spf13/cast"
	"github.com/zeromicro/go-zero/core/logc"
	"github.com/zeromicro/go-zero/core/logx"
)

type WithdrawViewListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 资产开关
func NewWithdrawViewListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WithdrawViewListLogic {
	return &WithdrawViewListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *WithdrawViewListLogic) WithdrawViewList(req *types.WithdrawViewListReq) (resp *types.WithdrawViewListRes, err error) {
	resData := make([]types.DataVIewListItemRes, 0)

	resp = &types.WithdrawViewListRes{List: resData}

	if _, ok := common.All[int64(rpc.ModelType_UserAsset)]; !ok {
		return nil, code.ParamsInvalid
	}

	u, err := WithdrawSettingGet(l.svcCtx, l.ctx, rpc.ModelType_UserAsset)

	if err != nil {
		logc.Error(l.ctx, "DataViewList err:", err)
		return nil, err
	}

	u2, err2 := WithdrawSettingGet(l.svcCtx, l.ctx, rpc.ModelType_UserBase)

	if err2 != nil {
		logc.Error(l.ctx, "DataViewList err:", err2)
		return nil, err2
	}

	for _, v := range u.Data.List {
		vs := map[string]interface{}{}
		_ = json.Unmarshal([]byte(v.Value), &vs)
		resData = append(resData, types.DataVIewListItemRes{
			ModelType: int64(v.ModelType),
			Value:     vs,
			Type:      v.Type,
		})

	}
	for _, v := range u2.Data.List {
		vs := map[string]interface{}{}
		_ = json.Unmarshal([]byte(v.Value), &vs)
		resData = append(resData, types.DataVIewListItemRes{
			ModelType: int64(v.ModelType),
			Value:     vs,
			Type:      v.Type,
		})

	}

	resp.List = resData
	return resp, nil

	return
}
func WithdrawSettingGet(svcCtx *svc.ServiceContext, ctx context.Context, ModelType rpc.ModelType) (resp *dataview.DataViewRes, err error) {

	if _, ok := common.All[int64(ModelType)]; !ok {
		return nil, code.ParamsInvalid
	}

	u, err := svcCtx.CommonSettingRpc.DataView(ctx, &dataview.DataViewReq{
		ModelType:  ModelType,
		MerchantId: MerchantIdGet(svcCtx.CommonSettingRpc, ctx),
	})

	if err != nil {
		logc.Error(ctx, "DataViewList err:", err)
		return nil, err
	}

	if u.Code != int64(code.SettingRpcSuccessful.Code()) {
		return nil, code.BusinessFailure
	}

	return u, nil
}
func IsOpen(svcCtx *svc.ServiceContext, ctx context.Context, in int64, value string) int64 {

	ModelType := in / 10000
	if _, ok := common.All[int64(ModelType)]; !ok {
		return 0
	}

	u, err := svcCtx.CommonSettingRpc.DataView(ctx, &dataview.DataViewReq{
		ModelType:  rpc.ModelType(ModelType),
		MerchantId: MerchantIdGet(svcCtx.CommonSettingRpc, ctx),
		Type:       in,
	})

	if err != nil {
		logc.Error(ctx, "DataViewList err:", err)
		return 0
	}

	if u.Code != int64(code.SettingRpcSuccessful.Code()) {
		return 0
	}
	for _, v := range u.Data.List {
		vs := map[string]interface{}{}
		_ = json.Unmarshal([]byte(v.Value), &vs)
		return cast.ToInt64(vs[value])
	}
	return 0
}
